$(document).ready(function(){
	loadDevModels();
});

function loadDevModels(){
	$("#devModelContainer").jtable({
		paging:true,
		pageSize:10,
		gotoPageArea:'none',
		pageSizeChangeArea:false,
		columnSelectable: false,//禁用选择显示列功能
		actions:{
			listAction:appCtx + "secure/nm/loadAllDevModels.html"
		},
		fields:{
			modelId:{
				key:true,
				list:false
			},
			modelName:{
				title:"网元型号",
				width:"5%"
			},
			typeName:{
				title:"网元类型",
				width:"5%"
			},
			manufactoryName:{
				title:"厂商",
				width:"3%"
			},
			oldName:{
				title:"默认用户名",
				width:"5%"
			},
			oldPwd:{
				title:"默认密码",
				width:"5%"
			},
			nmVlanId:{
				title:"网管VLANID",
				width:"5%"
			},
			biVlanId:{
				title:"业务VLANID",
				width:"5%"
			},
			loginPrompt:{
				title:"登陆用户名提示符",
				width:"5%"
			},
			pwdPrompt:{
				title:"登陆密码提示符",
				width:"5%"
			},
			commandPrompt:{
				title:"登陆成功提示符",
				width:"5%"
			},
			enablePwd:{
				title:"中兴ENABLE密码",
				width:"8%"
			},
			enablePrompt:{
				title:"中兴ENABLE密码成功提示符",
				width:"8%"
			},
			l2VEthName:{
				title:"二层接入虚拟端口前缀",
				width:"8%"
			},
			l3VEthName:{
				title:"三层接入虚拟端口前缀",
				width:"8%"
			},
			ctpNamePre:{
				title:"CTP名称前缀",
				width:"5%"
			},
			operation:{
				title:"操作",
				width:"20%",
				userData:true,
				content:$("#operation").html(),
				param:"modelId,modelName",
				listClass:"operation"
			}
		}
	});
	
	queryDevModel();
}

function queryDevModel(){
	var params = $("#queryForm").serialize();
	$("#devModelContainer").jtable("load",params);
}

function createDevModel(){
	var url = appCtx + "secure/nm/init.html";
	$.post(url,function(data){
		$("#nmModal .modal-body").html(data);
	});
	$("#nmModal").modal();
}

function saveDevModel(){
	var flag = $("#devModelForm").validationAjax();
	if(flag){
		var url = appCtx + "secure/nm/save.html";
		var params = $("#devModelForm").serialize();
		$.post(url,params,function(data){
			$("#devModelContainer").jtable("reload");
			$("#nmModal").modal("hide");
		});
	}
	return flag;
}

function update(modelId){
	var url = appCtx + "secure/nm/preUpdate.html";
	$.post(url,{modelId:modelId},function(data){
		$("#nmModal .modal-body").html(data);
	});
	$("#nmModal").modal();
}

function deleteDevModel(modelId,modelName){
	var url = appCtx + "secure/nm/delete.html";
	if(window.confirm("确定删除["+modelName+"]吗?")){
		$.post(url,{modelId:modelId},function(data){
			$("#devModelContainer").jtable("reload");
		});
	}
}

function loadDevTemplate(modelId,title){
	$("#devTemplateContainer").jtable({
		title:title+" 模板列表",
		paging:true,
		pageSize:8,
		gotoPageArea:'none',
		pageSizeChangeArea:false,
		columnSelectable: false,//禁用选择显示列功能
		toolbar:{
			hoverAnimation: true, //Enable/disable small animation on mouse hover to a toolbar item.    
			hoverAnimationDuration: 60, //Duration of the hover animation.    
			hoverAnimationEasing: undefined, //Easing of the hover animation. Uses jQuery's default animation ('swing') if set to undefined.    
			items: [{		//Array of your custom toolbar items.
				text:"新增",
				cssClass:"",
				click:function(){
					var url = appCtx + "secure/nm/initDevTemplate.html";
					$.post(url,{modelId:modelId},function(data){
						$("#devTemplateModal .modal-body").html(data);
					});
					$("#devTemplateModal").modal();
				}
			}] 
		},
		actions:{
			listAction: appCtx + "secure/nm/loadDevTemplates.html?modelId="+modelId
		},
		fields:{
			templateId:{
				key:true,
				list:false
			},
			name:{
				title:"模板名称",
				width:"5%"
			},
			modelName:{
				title:"网元型号",
				width:"5%"
			},
			typeName:{
				title:"网元类型",
				width:"5%"
			},
			manufactoryName:{
				title:"厂商",
				width:"5%"
			},
			newName:{
				title:"新增用户名",
				width:"5%"
			},
			newPwd:{
				title:"新增用户名密码",
				width:"5%"
			},
			ptp1x:{
				title:"1X业务标准配置端口",
				width:"10%"
			},
			doPtp:{
				title:"DO业务标准配置端口",
				width:"10%"
			},
			ltePtp:{
				title:"LTE业务标准配置端口",
				width:"10%"
			},
			commandType:{
				title:"命令行连接方式",
				width:"8%"
			},
			commandPort:{
				title:"命令行连接端口",
				width:"8%"
			},
			snmpCommunity:{
				title:"SNMP共同体串",
				width:"8%"
			},
			snmpPort:{
				title:"SNMP端口",
				width:"5%"
			},
			snmpVer:{
				title:"SNMP版本",
				width:"5%"
			},
			upPtp:{
				title:"上联口PTP",
				width:"5%"
			},
			downPtp:{
				title:"下联口PTP",
				width:"5%"
			},
			netPtp:{
				title:"BB互联PTP",
				width:"5%"
			},
			createDateStr:{
				title:"创建时间",
				width:"5%"
			},
			operation:{
				title:"操作",
				width:"20%",
				userData:true,
				content:$("#operationForDevTemplate").html(),
				param:"templateId,name",
				listClass:"operation"
			}
		}
	});
	
	queryDevTemplate();
}

function queryDevTemplate(){
	$("#devTemplateContainer").jtable("load");
}

function saveDevTemplate(){
	var flag = $("#devModelForm").validationAjax();
	if(flag){
		var url = appCtx + "secure/nm/saveDevTemplate.html";
		var params = $("#devTemplateForm").serialize();
		$.post(url,params,function(data){
			$("#devTemplateModal").modal("hide");
			$("#devTemplateContainer").jtable("reload");
		});
	}
	return flag;
}

function updateDevTemplate(templateId){
	var url = appCtx + "secure/nm/preUpdateDevTemplate.html";
	$.post(url,{templateId:templateId},function(data){
		$("#devTemplateModal .modal-body").html(data);
	});
	$("#devTemplateModal").modal();
}

function deleteDevTemplate(templateId,name){
	var url = appCtx + "secure/nm/deleteDevTemplate.html";
	if(window.confirm("确定删除["+name+"]吗?")){
		$.post(url,{templateId:templateId},function(){
			$("#devTemplateContainer").jtable("reload");
		});
	}
}

function configDevTemplate(modelId,modelName){
	var url = appCtx + "secure/nm/listDevTemplate.html";
	$.post(url,function(data){
		tabpanel.addTab({id:"devTemplate",title:"网元模板配置",html:data});
	}).done(function(){
		loadDevTemplate(modelId,modelName);
	});
}

var areaZTree;
var areaTreeSetting = {	
	data:{
		key:{
			name:"treeName"
		},
		simpleData:{
			enable:true,
			idKey:"treeId",
			pIdKey:"parentId",
			rootPId:0
		}
	},
	callback:{
		onClick:selectNode
	}
};

function initAreaZTree(){
	var url = appCtx + "secure/resconfig/qryAllResTree.html";
	$.post(url,function(data){ 
		moduleZTree=$.fn.zTree.init($("#areaZTree"), areaTreeSetting, data);
	});
}

/**
 * 选中一个节点
 */
function selectNode(event,treeId,treeNode){
	var treeId = treeNode.treeId;
	var treeName = treeNode.treeName;
	$("#areaId").val(treeId);
	$("#areaName").val(treeName);
}

/**
 * 显示资源树形数据
 */
function showArea(){
	initAreaZTree();
	var areaNameObj = $("#areaName");
	var areaNameOffset = areaNameObj.offset();
	$("#areaZTreeContent").css({left:areaNameOffset.left-304 + "px", top:areaNameOffset.top + areaNameObj.outerHeight() - 101 + "px"}).slideDown("fast");
	$("#devTemplateForm").bind("mousedown", onDevTemplateFormDown);
}

function hideMenu() {
	$("#areaZTreeContent").fadeOut("fast");
	$("body").unbind("mousedown", onDevTemplateFormDown);
}
function onDevTemplateFormDown(event) {
	if (!(event.target.id == "selectBtn" || event.target.id == "areaZTreeContent" || $(event.target).parents("#areaZTreeContent").length>0)) {
		hideMenu();
	}
}
